package com.hlw.springboot.controller;

import com.hlw.springboot.entity.SysConfig;
import com.hlw.springboot.mapper.SysConfigMapper;
import com.hlw.springboot.util.DataResult;
import com.hlw.springboot.util.R;
import com.hlw.springboot.util.ToLayuiTableUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统配置
 */
@RestController
@RequestMapping("/sysConfig")
public class SysConfigController {
    @Resource
    private SysConfigMapper sysConfigMapper;


    /**
     *
     * 用户列表
     */
    @PostMapping("/layui")
    @ApiOperation(value = "layui列表")
    public R layuiList(@RequestBody Map<String, Object> map) {
        ToLayuiTableUtil.toMysqlPage(map);
        ToLayuiTableUtil toLayuiTable = ToLayuiTableUtil.setMap(map);
        Map<String, Object> data = toLayuiTable.getData();
        List<SysConfig> list = sysConfigMapper.list(data, toLayuiTable.getPage(), toLayuiTable.getLimit());
        int count = sysConfigMapper.count(data);
        return R.ok().put("code", 0).put("data", list).put("count", count);
    }

    /**
     * layui列表进行编辑
     * @param
     * @param
     * @return
     */
    @PutMapping(value="updateById")
    public DataResult updateById(String id,String content,String fieldName) {
        Map<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("content",content);
        map.put("fieldName",fieldName);
        int count = sysConfigMapper.updateById(map);
        if(count>0){
            return new DataResult(1,"操作成功");
        }else {
            return new DataResult(404,"操作失败");
        }
    }

    /**
     * 获取所有系统配置
     * @return
     */
    @RequestMapping("/findAll")
    public  DataResult findAll() {
        List<SysConfig> SysConfigList = sysConfigMapper.findAll();
        return  new DataResult(1,"获取所有系统配置",SysConfigList);
    }
}
